#!/bin/sh
# This script need to be called at least one day every other time.
# You have to edit the TIME and DEL_TIME if you want to backup the db less than a day.

export ORACLE_BASE=/oracle/
export ORACLE_HOME=/oracle/product/11.2.0/db_1
export ORACLE_SID=fpyzgl
export PATH=$PATH:$ORACLE_HOME/bin:$HOME/bin

KEEP_DAYS=7

TIME=$(date +%Y%m%d)
DB_FILE="orcl${TIME}.dmp"
DB_LOG="orcl${TIME}.log"

DEL_TIME=$(date -d -${KEEP_DAYS}day +%Y%m%d)
DEL_DB_FILE="orcl${DEL_TIME}.dmp"
DEL_DB_LOG="orcl${DEL_TIME}.log"

LOCAL_PATH="/oracle/bf/"

DB_ADDR="10.104.129.4"
DB_PORT="1521"
DB_PATH="fpyzxt"
DB_USER="fpyzxt"
DB_PASS="fpyzxt_20160325"

FTP_ADDR="10.104.122.38"
FTP_PATH="/fpyzxtbf"
FTP_USER="fp"
FTP_PASS="LIUCHUNLI612"

echo "$TIME"
echo "Backup start ..."
exp ${DB_USER}/${DB_PATH}@${DB_ADDR}:${DB_PORT}/${DB_PATH} file=${LOCAL_PATH}${DB_FILE} log=${LOCAL_PATH}${DB_LOG} full=y
echo "Export completed"

# upload database and log file to ftp and delete files in ftp.
echo "Upload exported files $DB_FILE $DB_LOG delete outdated files $DEL_DB_FILE $DEL_DB_LOG ..."
ftp -n <<- EOF
open $FTP_ADDR
user $FTP_USER $FTP_PASS
lcd $LOCAL_PATH
cd $FTP_PATH
bin
prompt
mput $DB_FILE $DB_LOG
mdelete $DEL_DB_FILE $DEL_DB_LOG
bye
EOF
echo "Upload completed"

echo "Delete local outdated files $DEL_DB_FILE $DEL_DB_LOG ..."
# delete local outdated backup files
rm -f ${LOCAL_PATH}/${DEL_DB_FILE} ${LOCAL_PATH}/${DEL_DB_LOG}
echo "Backup completed.\n"
